﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirCeilingHeat.xaml
    /// </summary>
    public partial class ucAirCeilingHeat : UserControl
    {
        bool display = true;
        public ucAirCeilingHeat()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }
        public void loadData()
        {
            cbxCeilingLayer.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCeilingStructure.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxCeilingStructure.DisplayMemberPath = "description";
            cbxCeilingLayer.DisplayMemberPath = "layer_type";
            //cbxCeilingLayer.SelectedIndex = 0;
            //cbxCeilingStructure.SelectedIndex = 0;

        }



        private void cbxCeilingLayer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            displayHeat();
        }

        private void cbxCeilingStructure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            displayHeat();
        }

        private void displayHeat()
        {
            if ((cbxCeilingLayer.SelectedIndex >= 0 && cbxCeilingStructure.SelectedIndex >= 0) && display == true)
            {
                bool available = false;
                tb_ceiling cl = (tb_ceiling)cbxCeilingStructure.SelectedItem;
                tb_ceiling_layer cll = (tb_ceiling_layer)cbxCeilingLayer.SelectedItem;
                foreach (tb_heat_transfer_ceiling i in LibraryManagerInCom.CeilingCoefficientList)
                {
                    if (i.ceiling_id == cl.id && i.layer_id == cll.id)
                    {
                        txtHeat.Text = i.coefficient.ToString();
                        available = true;
                        return;
                    }
                }

                if (!available)
                {
                    txtHeat.Text = "Not available...";
                }
            }
        }
        public void addNew()
        {
            cbxCeilingLayer.SelectedIndex = -1;
            cbxCeilingStructure.SelectedIndex = -1;
            txtHeat.Text = "";
        }

        public void saveData()
        {
            tb_ceiling cl = (tb_ceiling)cbxCeilingStructure.SelectedItem;
            tb_ceiling_layer cll = (tb_ceiling_layer)cbxCeilingLayer.SelectedItem;
            tb_heat_transfer_ceiling htc = new tb_heat_transfer_ceiling();
            htc.ceiling_id = cl.id;
            htc.layer_id = cll.id;
            htc.coefficient = Convert.ToDouble(txtHeat.Text);
            LibraryManagerInCom.addCeilingHeat(htc);
        }
        public void deleteData()
        {
            tb_ceiling cl = (tb_ceiling)cbxCeilingStructure.SelectedItem;
            tb_ceiling_layer cll = (tb_ceiling_layer)cbxCeilingLayer.SelectedItem;
            tb_heat_transfer_ceiling htc = new tb_heat_transfer_ceiling();
            if (cl == null|| cll == null)
            {
                MessageBox.Show("All input must be not empty!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else
            {
                htc.layer_id = cl.id;
                htc.ceiling_id = cll.id;
                LibraryManagerInCom.delCeilingHeat(htc);
            }
            
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion


    }

}